/*
ID: icerupt1
PROG: skidesign
LANG: C++11
*/
#include <fstream>
#include <algorithm>
using namespace std;

int const maxn = 1001;
int da[maxn];
int n;

int cost_to_height(int h)
{
	int cost = 0;
	for (int i = 0; i < n; i++)
		if (da[i] > h) cost += (da[i]-h)*(da[i]-h);
		else
		if (da[i] < h-17) cost += (da[i]-h+17)*(da[i]-h+17);
	return cost;
}

int main()
{
	ifstream fin {"skidesign.in" };
	ofstream fout{"skidesign.out"};

	fin >> n;
	int min_h = 101, max_h = 0;
	for (int i = 0; i < n; i++) {
		fin >> da[i];
		if (da[i] < min_h) min_h = da[i];
		if (da[i] > max_h) max_h = da[i];
	}

	int cost = cost_to_height(max_h);
	for (int i = min_h+17+1; i < max_h; i++) {
		cost = min(cost_to_height(i), cost);
	}
	fout << cost << "\n";
}
